#!/bin/bash
delet_relation_sql="delete from t_relatin_thingboard where source_id in (select id from t_location)"
#获取系统token
USERNAME=$1
ENVIRON=$2
currentHome=$(cd "$(dirname "$0")";pwd)
YAPAI_HOME=$(dirname "$currentHome")
echo $YAPAI_HOME
IP=(`cat ${YAPAI_HOME}/enviroment/${ENVIRON} | jq -r '.ip'`)
DATABASE_USER_NAME=(`cat ${YAPAI_HOME}/enviroment/${ENVIRON} | jq -r '.database_user_name'`)
DATABASE_PASSWORD=(`cat ${YAPAI_HOME}/enviroment/${ENVIRON} | jq -r '.database_password'`)
DATABASE_PORT=(`cat ${YAPAI_HOME}/enviroment/${ENVIRON} | jq -r '.database_port'`)
ROOT="http://${IP}:8700"
HASH=`curl ${ROOT}/sld/random/getReadomMd5 | sed -ne 's/{"//p' |sed -ne's/"}//p'| awk -F "[ \" ]+" '{print $NF}'`
echo "获取的服务授权密钥:"${HASH}
# openssl md5
SHOWPASS="123456"
# PASS=`printf  ${SHOWPASS} | openssl md5 | awk '{print $NF}'`
PASS=`mysql -h${IP} -u${DATABASE_USER_NAME} -p${DATABASE_PASSWORD} -P${DATABASE_PORT} -e "use shuiliandong; select * from t_sys_user where user_name='ypkj'\G" 2>&1 | sed -n '2,$p' | grep -E 'password' | awk -F ':' '{print $2}'`
echo ${PASS}
LP=`printf  ${PASS}${HASH}|openssl md5 | awk '{print $NF}'`
echo "加密后:"$LP
TOKENMSG=`curl -X POST "${ROOT}/sld/oauth/token?username=${USERNAME}&password=${LP}&grant_type=password&client_id=Auth&client_secret=user123"`
# echo $TOKENMSG
# ACCESS_TOEKN=`echo $TOKENMSG | jq -r '.access_token'` 
# echo $ACCESS_TOEKN
TOKEN=`echo ${TOKENMSG} | grep -oE 'access_token":"[0-9a-zA-Z_.\-]+' | awk -F "[:\"]+" '{print $2}'`
#TOKEN=`echo ${TOKENMSG} | grep -E 'access_token":"[0-9a-zA-Z]+'`
LAST_TOKEN="Bearer "${TOKEN}
mysql -h ${IP} -uyapai -pypkj@1234 -D nanshan -e ''"${delet_relation_sql}"''
curl -si -H 'Authorization:'"${LAST_TOKEN}"'' 'http://'"${IP}"':8700/nanshan/location/list/syn'

